package com.taihua_tech.basic.aspect;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

//封装共同处理:将异常信息写入文件中(日志)
@Component
@Aspect
public class DaoExceptionHanlder {

	private static Logger logger = Logger.getLogger(DaoExceptionHanlder.class);
	
	//定义规则:public void 方法名(异常类型参数)//异常通知
	@AfterThrowing(throwing="e", pointcut="within(com.taihua_tech.basic.*.service..*)")
	public void handler(Exception e){
		logger.info("**************本次 service层 异常记录开始**********************");
		logger.info("异常类型:"+e);
		logger.info(e.getMessage()); 
		ByteArrayOutputStream baos = new ByteArrayOutputStream();		
		e.printStackTrace(new PrintStream(baos));
		String exception = baos.toString();	
		logger.error("exception:" + exception);
		logger.info("**************本次 service层  异常记录结束**********************");
	}
	
}
